﻿// CSysBackupDlg.cpp: 实现文件
//

#include "pch.h"
#include "HotelManagementMFC.h"
#include "afxdialogex.h"
#include "CSysBackupDlg.h"


// CSysBackupDlg 对话框

IMPLEMENT_DYNAMIC(CSysBackupDlg, CDialog)

CSysBackupDlg::CSysBackupDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_SYSBDLG, pParent)
{

}

CSysBackupDlg::~CSysBackupDlg()
{
}

void CSysBackupDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_FILEPATH, m_nFilePath);
}


BEGIN_MESSAGE_MAP(CSysBackupDlg, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_GETPATH, &CSysBackupDlg::OnBnClickedButtonGetpath)
	ON_BN_CLICKED(IDOK, &CSysBackupDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CSysBackupDlg 消息处理程序


void CSysBackupDlg::OnBnClickedButtonGetpath()
{
	CFileDialog dlg(true);
	if (dlg.DoModal() == IDOK)
	{
		m_nFilePath.SetWindowTextW(dlg.GetPathName());
	}
}


void CSysBackupDlg::OnBnClickedOk()
{
	if (MessageBox(L"是否开始备份文件？", L"提示", MB_OKCANCEL) == IDOK)
	{
		CFile* pSrcFile = NULL, * pDstFile = NULL;
		TRY{
			// 原始文件
			pSrcFile = new CFile(L"E:\\Programming-Sty\\C-C++\\Cpp\\MFC\\HotelManagementMFC\\res\\TESTDATA.mdb", CFile::modeRead);
		// 目标文件
		CString filePath;
		m_nFilePath.GetWindowText(filePath);
		pSrcFile = new CFile(filePath, CFile::modeCreate | CFile::modeWrite);
		// 读取字节数
		int nRead;
		// 复制缓冲区
		char szBuffer[1024];
		// 循环读取直到字节数为0
		while (nRead = pSrcFile->Read(szBuffer, sizeof(szBuffer)))
			pDstFile->Write(szBuffer, nRead);
		pSrcFile->Close();
		pDstFile->Close();
		delete pSrcFile, pDstFile;
		MessageBox(L"系统备份成功！", L"提示", MB_OK);
		}
			CATCH(CFileException, e)
		{
			MessageBox(L"系统备份失败！", L"提示", MB_OK);
			if (pSrcFile != NULL)
			{
				pSrcFile->Abort();
				delete pSrcFile;
			}
			if (pDstFile != NULL)
			{
				pDstFile->Abort();
				delete pDstFile;
			}
		}
		END_CATCH
	}

	CDialog::OnOK();
}
